<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="门店名称">
                <a-input placeholder="请输入门店名称" v-model="queryParam.name" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号">
                <a-input placeholder="请输入手机号" v-model="queryParam.mobile" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getShopList">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false" class="card_style_re">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.shop_audit" @change="getShopList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="0"> 未审核 </a-radio-button>
          <a-radio-button value="1"> 已审核 </a-radio-button>
          <a-radio-button value="2"> 已拒绝 </a-radio-button>
        </a-radio-group>
        <a-dropdown>
          <a-menu slot="overlay">
            <a-menu-item @click="allAudit(1)">批量同意</a-menu-item>
            <a-menu-item @click="allAudit(2)">批量拒绝</a-menu-item>
            <!-- <a-menu-item key="3" @click="allSynchronization">批量同步 </a-menu-item> -->
          </a-menu>
          <a-button> 批量操作 </a-button>
        </a-dropdown>
      </div>
      <a-alert type="info" show-icon style="margin-bottom: 10px" v-if="selectedRowKeys.length">
        <span slot="message">
          已选择 <span style="color: red">{{ selectedRowKeys.length }}</span> 项
        </span>
      </a-alert>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        :columns="columns"
        :data-source="shopList"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.cover_url" alt="" />
            </div>
            <div class="info">
              <span>{{ record.name }}</span>
            </div>
          </div>
        </div>
        <div slot="status" slot-scope="scope">
          <div class="status">
            <span :class="scope.shop_audit == 0 ? 'yellew' : scope.shop_audit == 1 ? 'aggree' : 'refuse'"></span>
            <span>{{ scope.shop_audit == 0 ? '待审核' : scope.shop_audit == 1 ? '已通过' : '已拒绝' }}</span>
          </div>
        </div>
        <span slot="created_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span slot="audit_time" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <div slot="action" slot-scope="text, scope">
          <div class="action">
            <a-button type="link" @click="audit(scope.id, 1)" size="small" :disabled="scope.shop_audit != 0"
              >通过</a-button
            >
            <span class="line"></span>
            <a-button type="link" @click="audit(scope.id, 2)" size="small" :disabled="scope.shop_audit != 0"
              >拒绝</a-button
            >
          </div>
        </div>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { auditList, batchAudit } from '@/api/outlet'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '门店名称',
    scopedSlots: {
      customRender: 'name',
    },
    width: 200,
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
  },
  {
    title: '门店地址',
    dataIndex: 'address',
    width: 120,
  },
  {
    title: '申请时间',
    dataIndex: 'created_at',
    scopedSlots: {
      customRender: 'created_at',
    },
    sorter: (a, b) => a.created_at - b.created_at,
  },
  {
    title: '审核时间',
    dataIndex: 'audit_time',
    scopedSlots: {
      customRender: 'audit_time',
    },
    sorter: (a, b) => a.audit_time - b.audit_time,
  },
  {
    title: '状态',
    width: 100,
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    width: 120,
    dataIndex: 'action',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  data() {
    return {
      // 查询参数
      queryParam: {
        name: null,
        mobile: null,
        shop_audit: '0',
        pageNo: 1,
        pageSize: 10,
      },
      // 已选中行的id
      selectedRowKeys: [],
      // 门店列表
      shopList: [],
      columns,
      // 数据总条数
      totalCount: 0,
    }
  },
  methods: {
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    // 获取门店列表
    async getShopList() {
      const hide = this.$message.loading('加载中', 0)
      const res = await auditList(this.queryParam)
      if (res.code == 0) {
        this.totalCount = res.data.totalCount
        this.shopList = res.data.data
      }
      hide()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getShopList()
    },
    // 同意或拒绝门店申请
    audit(id, statua) {
      let that = this
      let text = statua == 1 ? '通过' : '拒绝'
      this.$confirm({
        title: '是否' + text + '申请?',
        async onOk() {
          that.batchAudit([id], statua)
        },
        onCancel() {},
      })
    },
    // 批量操作门店
    async batchAudit(id, shop_audit) {
      const res = await batchAudit({
        id,
        shop_audit,
      })
      if (res.code == 0) {
        this.getShopList()
      }
    },
    // 点击批量同意或拒绝
    allAudit(statua) {
      if (this.selectedRowKeys.length) {
        let that = this
        this.$confirm({
          title: '是否' + statua == 1 ? '通过' : '拒绝' + '申请?',
          async onOk() {
            that.batchAudit(that.selectedRowKeys, statua)
          },
          onCancel() {},
        })
      }
    },
  },
  created() {
    this.getShopList()
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-top: 14px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-top: 14px;
    margin-right: 20px;
  }
  .ant-btn {
    margin-top: 14px;
  }
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .yellew {
    background-color: #faad14;
  }
  .refuse {
    background-color: #f5222d;
  }
  .aggree {
    background-color: #65c817;
  }
}
.action {
  display: flex;
  align-items: center;
  .line {
    width: 1px;
    height: 14px;
    background-color: #d8d8d8;
  }
}
</style>